package cn.itcast.bos.web.action;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletOutputStream;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.domain.Region;
import cn.itcast.bos.domain.Subarea;
import cn.itcast.bos.utils.FileUtils;
import cn.itcast.bos.web.action.base.BaseAction;

@Controller
@Scope("prototype")
public class SubareaAction extends BaseAction<Subarea>{
	
	//保存 subarea 
	public String saveSubarea(){
		subareaService.save(model);
		return "subarealist";
	}
	
	//分页查询
	public String pageQuery(){
		//管理分区组合查询
		String addresskey = model.getAddresskey();
		DetachedCriteria subareaDc = pageBean.getDetachedCriteria(); //获得离线对象引用
		//获得region 对象
		Region region = model.getRegion();
		
		if (StringUtils.isNotBlank(addresskey)) {
			subareaDc.add(Restrictions.like("addresskey", "%"+addresskey+"%"));
		}
		
		if (region !=null) {
			String province = region.getProvince();
			String city = region.getCity();
			String district = region.getDistrict();
			
			// 获得关联添加查询离线对象subareaDc.createCriteria("region")
			DetachedCriteria regionDc = subareaDc.createCriteria("region");
			
			if (StringUtils.isNotBlank(province)) {
				regionDc.add(Restrictions.like("province", "%"+province+"%"));
			}
			if (StringUtils.isNotBlank(city)) {
				regionDc.add(Restrictions.like("city", "%"+city+"%"));
			}
			if (StringUtils.isNotBlank(district)) {
				regionDc.add(Restrictions.like("district", "%"+district+"%"));
			}
			
		}
		
		subareaService.pageQueryList(pageBean);
		//排除序列化中 死循环的项   session 解决的延迟加载问题 时期能立即加载resigion 数据
		this.pageList(pageBean, new String[]{"pageSize","currentPage","detachedCriteria","subareas","decidedzone"});
		return NONE;
	}
	
	//ouPutFile 导出文件到 excel
	public String ouPutFile() throws IOException{
		// 首先查询出所有的分区 subarea   创一个sheet 使用文件下载 两个头一个流 导入到目的文件
		List<Subarea> list=subareaService.findAll();
		
		//将list 集合中的数据写入一个 Excel中
		HSSFWorkbook workbook = new HSSFWorkbook(); // 创建一个excel 文件 当前这个文件在内存中
		//创建一个 sheet 页
		 HSSFSheet sheet = workbook.createSheet("分区数据");
		//创建标题行
		 HSSFRow headRow = sheet.createRow(0);
		 //创建每列对应的 对象数据
		 headRow.createCell(0).setCellValue("分区编号");
		 headRow.createCell(1).setCellValue("区域编号");
		 headRow.createCell(2).setCellValue("关键字");
		 headRow.createCell(3).setCellValue("起始号");
		 headRow.createCell(4).setCellValue("结束号");
		 headRow.createCell(5).setCellValue("当前号");
		 headRow.createCell(6).setCellValue("位置信息");
		 		
		 //循环遍历list将数据徐如excel中
		 for (Subarea subarea : list) {
			 // 获得总的行号
			 HSSFRow dataRow = sheet.createRow(sheet.getLastRowNum()+1); 
			 dataRow.createCell(0).setCellValue(subarea.getId());
			 dataRow.createCell(1).setCellValue(subarea.getRegion().getId());
			 dataRow.createCell(2).setCellValue(subarea.getAddresskey());
			 dataRow.createCell(3).setCellValue(subarea.getStartnum());
			 dataRow.createCell(4).setCellValue(subarea.getEndnum());
			 dataRow.createCell(5).setCellValue(subarea.getSingle());
			 dataRow.createCell(6).setCellValue(subarea.getPosition());
		}
		 
		 //文件下载
		 ServletOutputStream outputStream = ServletActionContext.getResponse().getOutputStream();
		//设置文件名
		 String filename ="分区数据.xls";
		 // 下载文件时，针对不同浏览器，进行附件名的编码;    客户端浏览器 编码后的下载附件名
		 filename= FileUtils.encodeDownloadFilename(filename, 
				 ServletActionContext.getRequest().getHeader("user-agent"));
		 
		 // 获得文件的类型
		 ServletActionContext.getResponse().setContentType(
				 ServletActionContext.getServletContext().getMimeType(filename));
		 
		 //设置中文问题,
		 ServletActionContext.getResponse().setHeader("content-disposition", "attachment;filename="+filename);
		 workbook.write(outputStream);
		 
		 return NONE;
	}
	
	//selectSubarea 关联分区   为分配定区的分区
	public String selectSubarea(){
		DetachedCriteria dc = DetachedCriteria.forClass(Subarea.class);
		dc.add(Restrictions.isNull("decidedzone"));
		List<Subarea> list =subareaService.findByCriteria(dc);
		this.writeObjectFastjson(list, new String[]{"decidedzone","region"});
		return NONE;
	}
}
